package com.project.server.service.broadroom.impl;

import com.project.common.core.domain.BaseServer;
import com.project.server.domain.broadroom.TBroadroomBook;
import com.project.server.mapper.broadroom.TBroadroomBookMapper;
import com.project.server.service.broadroom.ITBroadroomBookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 会议室预约Service业务层处理
 *
 * @author project
 * @date 2021-06-22
 */
@Service
public class TBroadroomBookServiceImpl extends BaseServer implements ITBroadroomBookService {

    private static final Logger log = LoggerFactory.getLogger(TBroadroomBookServiceImpl.class);

    @Resource
    private TBroadroomBookMapper tBroadroomBookMapper;

    /**
     * 查询会议室预约
     *
     * @param broadroomBookId 会议室预约ID
     * @return 会议室预约
     */
    @Override
    public TBroadroomBook selectTBroadroomBookById(Long broadroomBookId) {
        return tBroadroomBookMapper.selectTBroadroomBookById(broadroomBookId);
    }

    /**
     * 根据预约人电话号码查询会议室预约记录
     *
     * @return 会议室预约记录
     */
    @Override
    public List<TBroadroomBook> selectTBroadroomBookByPhone(TBroadroomBook tBroadroomBook) {
        return tBroadroomBookMapper.selectTBroadroomBookByPhone(tBroadroomBook);
    }

    /**
     * 查询会议室预约列表
     *
     * @param tBroadroomBook 会议室预约
     * @return 会议室预约
     */
    @Override
    public List<TBroadroomBook> selectTBroadroomBookList(TBroadroomBook tBroadroomBook) {
        return tBroadroomBookMapper.selectTBroadroomBookList(tBroadroomBook);
    }

    @Override
    public List<TBroadroomBook> selectTBroadroomBookBybookStart(TBroadroomBook tBroadroomBook) {
        return tBroadroomBookMapper.selectTBroadroomBookBybookStart(tBroadroomBook);
    }

    /**
     * 新增会议室预约
     *
     * @param tBroadroomBook 会议室预约
     * @return 结果
     */
    @Override
    public int insertTBroadroomBook(TBroadroomBook tBroadroomBook) {
        Date bookStart = tBroadroomBook.getBookStart();
        Date bookEnd = tBroadroomBook.getBookEnd();
        if (bookStart.getTime() > bookEnd.getTime()) {
            Date timeTemp = null;
            timeTemp = bookStart;
            bookStart = bookEnd;
            bookEnd = timeTemp;
        }
        tBroadroomBook.setBookEnd(bookEnd);
        tBroadroomBook.setBookStart(bookStart);
        super.insert(tBroadroomBook);
        return tBroadroomBookMapper.insertTBroadroomBook(tBroadroomBook);

    }

    /**
     * 修改会议室预约
     *
     * @param tBroadroomBook 会议室预约
     * @return 结果
     */
    @Override
    public int updateTBroadroomBook(TBroadroomBook tBroadroomBook) {
//        if(tBroadroomBook.getApproverState().equals("2")){
//            super.update(tBroadroomBook);
//            return tBroadroomBookMapper.updateTBroadroomBook(tBroadroomBook);
//        }else {
//            List<TBroadroomBook> tBroadroomBooks = tBroadroomBookMapper.selectTBroadroomBookBybookStart(tBroadroomBook);
//            if (tBroadroomBooks.size() > 0) {
//                return 0;
//            }
            super.update(tBroadroomBook);
            return tBroadroomBookMapper.updateTBroadroomBook(tBroadroomBook);
        //}
    }

    /**
     * 修改预约状态
     *
     * @param tBroadroomBook 会议室预约
     * @return 结果
     */
    @Override
    public int updateBookStatus(TBroadroomBook tBroadroomBook) {
        super.update(tBroadroomBook);
        return tBroadroomBookMapper.updateTBroadroomBook(tBroadroomBook);
    }


    /**
     * 批量删除会议室预约
     *
     * @param broadroomBookIds 需要删除的会议室预约ID
     * @return 结果
     */
    @Override
    public int deleteTBroadroomBookByIds(Long[] broadroomBookIds) {
        return tBroadroomBookMapper.deleteTBroadroomBookByIds(broadroomBookIds);
    }

    /**
     * 删除会议室预约信息
     *
     * @param broadroomBookId 会议室预约ID
     * @return 结果
     */
    @Override
    public int deleteTBroadroomBookById(Long broadroomBookId) {
        return tBroadroomBookMapper.deleteTBroadroomBookById(broadroomBookId);
    }

}
